const binance = require('node-binance-api');
binance.options({
    APIKEY: 'XGhpajaXEKeUm6piPg6hBYdnFQqCdLoSrYbuh9smQsFaor0QYyibxywMkMxJmIA2',
    APISECRET: 'prgief3wM00BUaNeBFly1x1fX5ex71PP54wVxPufsVg2qA6Eq2O5Fj14LzFRKpm8',
    useServerTime: true, // If you get timestamp errors, synchronize to server time at startup
    test: true // If you want to use sandbox mode where orders are simulated
});

let symbols = ['XLMBTC', 'ETHBTC', 'LENDBTC', 'GXSBTC', 'VENBTC', 'AEBTC', 'ONTBTC', 'ZEC', 'PPT'];
let orders = [];
// For all symbols:
binance.websockets.candlesticks(symbols, "15m", (candlesticks) => {
    let {e: eventType, E: eventTime, s: symbol, k: ticks} = candlesticks;
    let {o: open, h: high, l: low, c: close, v: volume, n: trades, i: interval, x: isFinal, q: quoteVolume, V: buyVolume, Q: quoteBuyVolume} = ticks;

//console.log("open: "+open);
//console.log("high: "+high);
//console.log("low: "+low);
//console.log("close: "+close);
//console.log("volume: "+volume);
//console.log("isFinal: "+isFinal);
    percent = (close - open) / open * 100;
    percent1 = (low - high) / high * 100;
    console.log(symbol + " " + interval + " candlestick update:" + percent1);
//console.log(symbol+"percent:"+percent)
//console.log(symbol+"max:"+percent1)
    if (percent < -20 && !orders[symbol]) {
        orders[symbol] = close;
        console.log("开始买进 价格：" + close);
    }
    percent2 = 0;
    if (orders[symbol]) {
        percent2 = (close - orders[symbol]) / orders[symbol] * 100;
        if (percent2 > 1) {
            orders[symbol] = false;
            console.log("开始卖出 价格：" + close);
        }
    }
});


//var quantity = 1, price = 0.069;
//binance.buy("ETHBTC", quantity, price);
//binance.sell("ETHBTC", quantity, price);

//var quantity = 5, price = 0.00402030;
//binance.buy("BNBETH", quantity, price, {type:'LIMIT'}, (error, response) => {
//    console.log("Limit Buy response", response);
//console.log("order id: " + response.orderId);
//});